/**
 * 
 */
package com.jie.eclipse.pan.editors.assist.contributor;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.wst.html.core.text.IHTMLPartitions;
import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument;
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMAttr;
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement;

import com.jie.eclipse.pan.editors.assist.data.Item;
import com.jie.eclipse.pan.utils.StringHelper;
import com.jie.eclipse.pan.utils.Utils;

/**
 * @author yanjie
 *
 */
public class BaseFormItemAssContributor extends AbstractAssistantContributor{
	private List<Item> attrList = null;
	private List<Item> attrValueList = null;

	/**
	 * 
	 */
	public BaseFormItemAssContributor() {
		initAttr();
		initAttrValue();
	}
	
	public void initAttr(){
		attrList = new ArrayList<Item>();
		attrList.add(new Item("caption", "caption=\"\""));
		attrList.add(new Item("captionWidth", "captionWidth=\"\""));
		attrList.add(new Item("captionDisplay", "captionDisplay=\"\""));
		attrList.add(new Item("captionCss", "captionCss=\"\""));
		attrList.add(new Item("captionStyle", "captionStyle=\"\""));
		attrList.add(new Item("colspan", "colspan=\"\""));
		attrList.add(new Item("required", "required=\"true\""));
	}
	
	public void initAttrValue(){
		attrValueList = new ArrayList<Item>();
	}
	
	@SuppressWarnings("restriction")
	@Override
	protected Item[] computeAttributes(IDOMElement element, IDOMAttr attrNode,String tagName,String attrbuteName, String inputText, ITextViewer viewer, int offset) {
		//Utils.print("attrbuteName="+attrbuteName);
		IStructuredDocument structuredDocument = (IStructuredDocument)viewer.getDocument();
		List<Item> items = new ArrayList<Item>();
		try {
			if(IHTMLPartitions.HTML_DEFAULT.equals(structuredDocument.getPartition(offset).getType())){
				for (int i = 0; i < this.getAttrList().size(); i++) {
					if(StringHelper.IsNullOrEmpty(attrbuteName) || this.getAttrList().get(i).getText().toLowerCase().trim().startsWith(attrbuteName.toLowerCase().trim())){
						items.add(this.getAttrList().get(i));
					}
				}
			}
		} catch (BadLocationException e) {
			e.printStackTrace();
		}
		return items.toArray(new Item[items.size()]);
	}
	
	@Override
	protected Item[] computeAttributeValues(IDOMElement element, IDOMAttr attrNode,String tagName,String attrbuteName, String inputText, ITextViewer viewer, int offset) {
		return super.computeAttributeValues(element,  attrNode, tagName, attrbuteName,  inputText,  viewer,  offset);
	}
	
	public List<Item> getAttrList() {
		return attrList;
	}
}
